package com.nq.utils;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 英为财情
 */
public class InvestingUtil {

    /**
     * @param countryid (新加坡-36 印度-14 日本-35  泰股-41)
     * @param page
     * @return
     */
    public static JSONArray getStocks(int countryid, int page) {
        Map<String, Object> params = new HashMap<>();
        params.put("fields-list", "id,name,symbol,open,high,low,last,lastPairDecimal,change,changePercent,volume,time,isOpen,url,flag,countryNameTranslated,exchangeId,performanceDay,performanceWeek,performanceMonth,performanceYtd,performanceYear,performance3Year,technicalHour,technicalDay,technicalWeek,technicalMonth,avgVolume,fundamentalMarketCap,fundamentalRevenue,fundamentalRatio,fundamentalBeta,pairType");
        params.put("country-id", countryid);
        params.put("page", page);
        params.put("page-size", 100);
        params.put("include-major-indices", false);
        params.put("include-additional-indices", false);
        params.put("include-primary-sectors", false);
        params.put("include-other-indices", false);
        params.put("limit", 0);
        String resp = CloudbyPassUtil.httpGet("https://api.investing.com/api/financialdata/assets/equitiesByCountry/default", params);
        JSONObject obj = JSONUtil.parseObj(resp);
        JSONArray data = obj.getJSONArray("data");
        if (data == null) {
            data = new JSONArray();
        }
        return data;
    }

    public static JSONArray kline(int id, String type) {
        String url = String.format("https://api.investing.com/api/financialdata/%s/historical/chart/?interval=%s&pointscount=110", id, type);
        String resp = CloudbyPassUtil.httpGet(url, null);
        JSONObject obj = JSONUtil.parseObj(resp);
        return obj.getJSONArray("data");
    }


    public static void main(String[] args) {
        System.out.println(getStocks(35, 0));
        //System.out.println(kline(8955, "PT5M"));
    }
}
